//
//  YHEmoticonToolbar.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/7/4.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit

//btn tag中间值
private let tagNum = 1000

//表情键盘类型标记
enum YHEmoticonToolBarType:Int {
    //默认
    case Deafault = 0
    //Emoji表情
    case Emoji = 1
    //浪小花
    case Lxh = 2
}

class YHEmoticonToolbar: UIView {
    
    //声明一个闭包告诉外界按钮点击了
    var toolBarClosure:((type: YHEmoticonToolBarType)->())?
    
    var currentBtn:UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //恭外界调用的方法
    func setCurrentButtons(tag: Int){
        let button = viewWithTag(tag + tagNum) as! UIButton
        //如果点击的button是当前button那么直接返回
        if currentBtn == button{
            return
        }
        
        button.selected = true
        currentBtn?.selected = false
        currentBtn = button
    }
    
    private func setupUI(){
        backgroundColor = RandomColor()
        let defaultButton = addChildButtons("left", title: "默认",type: .Deafault)
        let emojiButton = addChildButtons("mid", title: "Emoji",type: .Emoji)
        let lxhButton = addChildButtons("right", title: "浪小花",type: .Lxh)
        
        //添加约束
        defaultButton.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(emojiButton)
        }

        emojiButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultButton.snp_trailing)
            make.width.equalTo(lxhButton)
        }

        lxhButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emojiButton.snp_trailing)
        }
        
        defaultButton.selected = true
        currentBtn = defaultButton 
        
    }
    
    //创建button的公共方法
    func addChildButtons(imgName:String, title:String, type:YHEmoticonToolBarType)-> UIButton{
        let button = UIButton()
        
        button.tag = type.rawValue + tagNum
        
        //添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
        
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_selected"), forState: UIControlState.Selected)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        self.addSubview(button)
        
        return button
    }
    
    //监听点击事件
    @objc private func buttonClick(button:UIButton){
        //如果点击的button是当前button那么直接返回
        if currentBtn == button{
            return
        }
        
        button.selected = true
        currentBtn?.selected = false
        currentBtn = button
        
        toolBarClosure?(type: YHEmoticonToolBarType(rawValue: button.tag - tagNum)! )
    }
}













